package com.icode.web.controller.manager;

import com.icode.core.dto.manager.ReserveDTO;
import com.icode.core.shared.ReserveDateHandle;
import com.icode.service.house.ReserveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-4-15
 * Time: 上午9:58
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/reserve")
public class ReserveController {

    @Autowired
    private ReserveService reserveService;

    @RequestMapping(value = "show/{myHouseGuid}", method = RequestMethod.GET)
    public ModelAndView show(@PathVariable String myHouseGuid, Model model) {
        ReserveDTO reserveDTO = reserveService.createReserveDTO(myHouseGuid);
        Set<Integer> months = ReserveDateHandle.getMonths(reserveDTO);
        model.addAttribute("myHouseGuid", myHouseGuid);
        model.addAttribute("reserveDTO", reserveDTO);
        model.addAttribute("months", months);
        return new ModelAndView("/front/manager/reserve", "reserveDTO", reserveDTO);
    }

    @RequestMapping(value = "form", method = RequestMethod.POST)
    @ResponseBody
    public Object form(String occupancyName, String IDCard, String phoneNum, String email, String peers, String remarks, String myHouseGuid, String guids) {
        return reserveService.addReserveDTO(occupancyName, IDCard, phoneNum, email, peers, remarks, myHouseGuid, guids);
    }

    @RequestMapping(value = "back/list", method = RequestMethod.GET)
    public String list(Model model) {
        List<ReserveDTO> reserveDTOs = reserveService.findAllReServe();
        model.addAttribute("reserveDTOs", reserveDTOs);
        return "back/hotel/reserveOverview";
    }

    @RequestMapping(value = "changeStatus/{status}/{guid}", method = RequestMethod.GET)
    @ResponseBody
    public String changeStatus(@PathVariable String status, @PathVariable String guid) {
        reserveService.changeReserveStatus(status, guid);
        return "success";
    }


    @RequestMapping(value = "house_week_time_by_month/{month}/{houseWeekGuid}", method = RequestMethod.GET)
    @ResponseBody
    private Object getHouseWeekTime(@PathVariable String month, @PathVariable String houseWeekGuid, HttpServletResponse response) {
        return reserveService.houseWeekTimeByMonth(month, houseWeekGuid, response);
    }

    @RequestMapping(value = "rent/{rentGuid}")
    private ModelAndView reserveForRent(@PathVariable String rentGuid) {
        ReserveDTO reserveDTO = reserveService.createReserveDTOForRent(rentGuid);
        return new ModelAndView("", "", reserveDTO);
    }

    @RequestMapping("weekTimes/{houseInfoGuid}")
    private void getWeekTimes(@PathVariable String houseInfoGuid, HttpServletResponse response) {
        reserveService.getWeekTimesByHouseInfoGuid(houseInfoGuid, response);
    }

    @RequestMapping(value = "searchBooting/{username}", method = RequestMethod.GET)
    @ResponseBody
    public List<ReserveDTO> searchBooting(@PathVariable String username, Model model) {
        List<ReserveDTO> reserveDTOs = reserveService.findReserveByUserInfoUsername(username);
        return reserveDTOs;
    }
}
